home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7715 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: more problems with qsort
  5. Date: 27 Feb 1996 15:44:36 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb27084436@qcd.lanl.gov>
  8. References: <177399702S86.JW1675A@american.edu>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: JW1675A@american.edu's message of Mon, 26 Feb 96 10:44:18 EST
  13.  
  14. In article <177399702S86.JW1675A@american.edu>
  15. JW1675A@american.edu (James D. Watson) writes:
  16.  
  17. <snip>
  18. JDW: Architecture: SunOS 4.1.3 with ANSI-C compiler.
  19. JDW:  
  20. JDW: I'm doing some file manipulation and between steps A and B, I
  21. need to sort. 
  22. JDW: For various reasons, I don't want to popen() to the sort
  23. utility--I want to 
  24. JDW: use qsort.  Here's what I'm doing:
  25. JDW:  
  26. JDW:    get number of lines in the file
  27. JDW:    (char**)malloc with enough room for all lines
  28. JDW:    for each line in the file {
  29. JDW:      (char*)malloc(90)  /* 90 is enough room for each line in file */
  30. JDW:      copy each line into the newly malloc()ed space
  31. JDW:      point a (char**) to the newly malloc()ed space
  32. JDW:    }
  33.  
  34. The above code block does not compile. 
  35.  
  36. JDW:  
  37. JDW: so now I should have "lines" number of pointers to pointers to char,
  38. JDW: each one pointing to 90 bytes containing a line in the file.
  39. JDW:  
  40. JDW: So, I call qsort(array[0], lines, 90, compare)
  41. JDW: where compare is my comparison function -- prepared as discussed
  42. JDW: in the FAQ.  Now all I get are core dumps during the call to qsort().
  43. JDW: :-)
  44.  
  45. Please write your code where every function has a `prototype' either
  46. by inclusion of a standard header (e.g. <stdlib.h>) or directly.
  47. Write your code using no casts. Get it to a point where the compiler
  48. does not complain. 
  49.  
  50. Then readup the difference between char** and char[][90] (the FAQ
  51. should explain this well, and this is the problem with your
  52. code). Read up the documentation of qsort.
  53.  
  54. If you still do not understand it, please write the shortest possible
  55. such code that demonstrates the problem. Then post the resulting code
  56. if you still have trouble.
  57.  
  58. JDW:  
  59. JDW: The core dump isn't in my compare routine -- it's somewhere else in
  60. JDW: the qsort() function according to dbx -- so I'm sure it's a problem
  61. JDW: setting up my data.
  62. JDW:  
  63. JDW: Any tips?  Much appreciation in advance,
  64.  
  65. Please take the time to ask the question in a meaningful form so as
  66. more people can help you.
  67.  
  68. Cheers
  69. Tanmoy
  70. --
  71. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  72. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  73. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  74. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  75. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  76. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  77.